package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Class;
import com.example.demo.entity.Course;
import com.example.demo.entity.CourseAllocation;
import com.example.demo.entity.Teacher;
import com.example.demo.mapper.CourseMapper;
import com.example.demo.service.*;
import com.example.demo.vo.courseSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author
 * @since 2025-11-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseAllocationService courseAllocationService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IClassService classService;
    @Autowired
    private IUserService userService;

    //TODO ！！！大连查，做缓存，一定要做！！！
    @Override
    public List<courseSelectVo> queryE() {
        return this.list().stream().map(course -> {
            courseSelectVo vo = new courseSelectVo();
            vo.setCourseId(course.getCourseId());
            vo.setCourseName(course.getCourseName());
            vo.setCourseCode(course.getCourseCode());
            vo.setCredit(course.getCredit());
            vo.setDescription(course.getDescription());
            vo.setCreateTime(course.getCreateTime());
            List<courseSelectVo.Allocation> courseId = courseAllocationService.list(new QueryWrapper<CourseAllocation>().eq("course_id", course.getCourseId()))
                    .stream().map(courseAllocation -> {
                        courseSelectVo.Allocation allocation = new courseSelectVo.Allocation();

                        allocation.setTeacherId(courseAllocation.getTeacherId());
                        Teacher byId = teacherService.getById(courseAllocation.getTeacherId());
                        allocation.setTeacherName(byId.getUserId() == null ? "" : userService.getById(byId.getUserId()).getUsername());

                        allocation.setClassId(courseAllocation.getClassId());
                        Class byId1 = classService.getById(courseAllocation.getClassId());
                        allocation.setClassName(byId1.getClassName() == null ? "" : byId1.getClassName());

                        allocation.setSemester(courseAllocation.getSemester());
                        return allocation;
                    }).toList();
            vo.setCourseAllocations(courseId);
            return vo;
        }).toList();
    }
}
